package com.hifar.test.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.common.QueryParamBean;
import com.hifar.fw.utils.lang.StringUtils;
import com.hifar.model.VO.QueryParamsVO;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.MinioUtil;
import com.hifar.sys.PageResult;
import com.hifar.sys.SystemUtil;
import com.hifar.test.api.model.ReportApiResultVO;
import com.hifar.test.api.model.ReportQueryVO;
import com.hifar.test.entrust.pojo.HfEnvGkEntrust;
import com.hifar.test.report.pojo.HfEnvReport;
import com.hifar.test.report.pojo.VO.HfEnvReportVO;
import com.hifar.test.report.service.IHfEnvReportService;
import com.hifar.test.test.pojo.HfEnvTest;
import com.hifar.test.test.service.IHfEnvTestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhuWeiWei
 * @description 报告查询接口
 * @date 2025/10/27 15:12
 */
@RestController
@RequestMapping("/api/busi/test/api/report-api")
@Api(tags = "报告查询接口")
public class ReportApiController {
    @Autowired
    private IHfEnvReportService envReportService;
    @Autowired
    private IHfEnvTestService envTestService;
    @Autowired
    private MinioUtil minioUtil;

    @PostMapping("/listPage")
    @ApiOperation("报告模块:列表查询")
    public BusiResponse listPage(@RequestBody ReportQueryVO reportQueryVO) {
        Page page = new Page(reportQueryVO.getPageNo(), reportQueryVO.getPageSize());

        QueryWrapper<ReportApiResultVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(reportQueryVO.getReportCode()), "r.report_code", reportQueryVO.getReportCode());
        queryWrapper.eq(StringUtils.isNotBlank(reportQueryVO.getEntrustCode()), "e.entrust_code", reportQueryVO.getEntrustCode());
        queryWrapper.like(StringUtils.isNotBlank(reportQueryVO.getEntrustCust()), "e.entrust_cust", reportQueryVO.getEntrustCust());
        queryWrapper.like(StringUtils.isNotBlank(reportQueryVO.getEntrustUserName()), "e.entrust_user_name", reportQueryVO.getEntrustUserName());


        Page<ReportApiResultVO> pageData = envReportService.pageApiVO(page, queryWrapper);


//        数据中台/ESB集成报告，开放报告查询接口，查询信息：报告编号、报告编制日期、报告下载路径）、委托单号、委托单位、委托人
//        、试验开始时间（第一个试验开始的时间）、试验结束时间（最后一个试验结束的时间）、试验项目（多个去重逗号分割）。
//        报告下载需约定加密方式
        List<ReportApiResultVO> records = pageData.getRecords();
        for (ReportApiResultVO record : records) {

            String gkEntrustId = record.getGkEntrustId();
            String entrustId = record.getEntrustId();

            String pdfPath = record.getPdfPath();
            String wordPath = record.getWordPath();
            Integer secretLevel = record.getSecretLevel();
            String newPdfPath = minioUtil.getFullFilePath(MinioUtil.PRE_DEFAULT, pdfPath, secretLevel);
            String newWordPath = minioUtil.getFullFilePath(MinioUtil.PRE_DEFAULT, wordPath, secretLevel);
            record.setPdfPath(newPdfPath);
            record.setWordPath(newWordPath);
        }
        /*计算分页信息返回给前端*/
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }

}
